//
//  AfNetWorkingHandler.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "AfNetWorkingHandler.h"
#import <AFNetworking.h>
@implementation AfNetWorkingHandler
+(void)GetWithStr:(NSString *)str complection:(NETBLOCK)netBlock
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        netBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Error!: %@", error);
    }];
}
+(void)PostWithStr:(NSString *)str body:(NSString *)body complection:(NETBLOCK)network
{
    //将body体拆分成字典
    NSArray *arr = [body componentsSeparatedByString:@"&" ];
    NSMutableArray *jiArr = [NSMutableArray array];
    NSMutableArray *ouArr = [NSMutableArray array];
    for (NSInteger i = 0; i < arr.count; i++) {
        NSString *str = arr[i];
        NSArray *a = [str componentsSeparatedByString:@"="];
        [jiArr addObject:a.firstObject];
        [ouArr addObject:a.lastObject];
        
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    for (NSInteger j = 0; j < arr.count; j++) {
        [dic setObject:ouArr[j] forKey:jiArr[j]];
    }
   
    //申明返回的结果是json类型
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //申明请求的数据是json类型
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:str parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //responseObject是NSdata 要转换成NSJSON
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        network(result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Error:%@", error);
    }];
 
}
@end
